package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.DwConstant;
import com.ruoyi.system.domain.DwScore;
import com.ruoyi.system.domain.DwTaskContent;
import com.ruoyi.system.domain.vo.ScoreListReqVO;
import com.ruoyi.system.domain.vo.ScoreListResVO;
import com.ruoyi.system.service.IDwScoreService;
import com.ruoyi.system.service.IDwTaskContentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DwWork;
import com.ruoyi.system.service.IDwWorkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考核内容Controller
 * 
 * @author ruoyi
 * @date 2024-01-19
 */
@RestController
@RequestMapping("/system/work")
public class DwWorkController extends BaseController
{
    @Autowired
    private IDwWorkService dwWorkService;

    @Autowired
    private IDwScoreService dwScoreService;

    @Autowired
    private IDwTaskContentService dwTaskContentService;


    /**
     * 评分榜单
     * @param reqVO
     * @return
     */
    @GetMapping("/score/list")
    public AjaxResult scoreList(ScoreListReqVO reqVO)
    {
        List<ScoreListResVO> list = dwScoreService.selectScoreList(reqVO);

        JSONObject result = new JSONObject();
        result.put("rank", list.stream().limit(10).collect(Collectors.toList()));
        Map<Long, ScoreListResVO> map = list.stream().collect(Collectors.toMap(ScoreListResVO::getUserId, Function.identity()));
        if (map.containsKey(getUserId())) {
            result.put("user", map.get(getUserId()));
        }

        return AjaxResult.success(result);
    }
    /**
     * 查询考核内容列表
     */
    @PreAuthorize("@ss.hasPermi('clockin:work:list')")
    @GetMapping("/list")
    public TableDataInfo list(DwWork dwWork)
    {
        startPage();
        List<DwWork> list = dwWorkService.selectDwWorkList(dwWork);
        return getDataTable(list);
    }

    /**
     * 查询考核内容列表
     */
    @PreAuthorize("@ss.hasPermi('clockin:work:list')")
    @GetMapping("/my/list")
    public TableDataInfo myList(DwWork dwWork)
    {
        dwWork.setCreatorId(getUserId());
        startPage();
        List<DwWork> list = dwWorkService.selectDwWorkList(dwWork);
        return getDataTable(list);
    }

    /**
     * 导出考核内容列表
     */
    @PreAuthorize("@ss.hasPermi('clockin:work:export')")
    @Log(title = "考核内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DwWork dwWork)
    {
        List<DwWork> list = dwWorkService.selectDwWorkList(dwWork);
        ExcelUtil<DwWork> util = new ExcelUtil<DwWork>(DwWork.class);
        util.exportExcel(response, list, "考核内容数据");
    }

    /**
     * 获取考核内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('clockin:work:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId)
    {
        return AjaxResult.success(dwWorkService.selectDwWorkByWorkId(workId));
    }

    /**
     * 新增考核内容
     */
    @PreAuthorize("@ss.hasPermi('clockin:work:add')")
    @Log(title = "考核内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DwWork dwWork)
    {
        dwWork.setCreatorId(getUserId());
        dwWork.setStatus(DwConstant.CHECK);
        dwWork.setDeptId(getDeptId());
        return toAjax(dwWorkService.insertDwWork(dwWork));
    }

    /**
     * 修改考核内容
     */
    @PreAuthorize("@ss.hasPermi('clockin:work:edit')")
    @Log(title = "考核内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DwWork dwWork)
    {
        dwWork.setCreatorId(getUserId());
        dwWork.setStatus(DwConstant.CHECK);
        return toAjax(dwWorkService.updateDwWork(dwWork));
    }


    /**
     * 审核内容
     */
    @PreAuthorize("@ss.hasPermi('clockin:work:edit')")
    @Log(title = "考核内容", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    @Transactional
    public AjaxResult audit(@RequestBody DwWork dwWork) {
        DwWork  old = dwWorkService.selectDwWorkByWorkId(dwWork.getWorkId());
        if (old.getStatus().equals(DwConstant.PASS)){
            return AjaxResult.error("已经审核通过了，不能再次审核");
        }

        if (dwWork.getStatus().equals(DwConstant.PASS)){
            dwWorkService.updateDwWork(dwWork);
            DwTaskContent taskContent = dwTaskContentService.selectDwTaskContentByContentId(old.getTaskContentId());
            DwScore score = new DwScore();
            score.setUserId(old.getCreatorId());
            score.setContentScore(taskContent.getContentScore());
            score.setContentId(dwWork.getWorkId());
            score.setTaskId(dwWork.getTaskId());
            score.setDisabled("0");
            dwScoreService.insertDwScore(score);
            return AjaxResult.success("审核通过");
        }else{
            dwWorkService.updateDwWork(dwWork);
            return AjaxResult.success("审核不通过");
        }

    }


        /**
         * 删除考核内容
         */
    @PreAuthorize("@ss.hasPermi('clockin:work:remove')")
    @Log(title = "考核内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds)
    {
        return toAjax(dwWorkService.deleteDwWorkByWorkIds(workIds));
    }
}
